<?php
/**
 * 学生信息显示
 */
//session_start();
include "../util/DBUtil.php";
//$login_type = $_SESSION['login_type'];  //登录类型
//$login_user = unserialize($_SESSION['login_user']);//反解序列获登录用户信息
?>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<div class="stuInfo">
    <table class="table table-striped">
        <tr>
            <th>ID</th>
            <th>学号</th>
            <th>姓名</th>
            <th>性别</th>
            <th class="<?php echo $login_type!=1? 'hidden':''?>">编辑</th>
        </tr>
        <?php
        $db = new DBUtil();
        $sql = "";
        $result = array();//存放结果集
        //关键字查询
        if ($_GET['action']=='key'){
            $key_type = $_POST['key_type'];
            $key = $_POST['key'];
            if ($key_type==0){//按学号查询
                //教师查询
                 if ($login_type==2){
                     $sql = "select * from tb_student where grade_id=? and student_no like ? ";
                     $params = array($login_user['grade_id'],'%'.$key.'%');
                 }
                 //管理员查询
                 else{
                     $sql = "select * from tb_student where student_no like ? ";
                     $params = array('%'.$key.'%');
                 }
                $result = $db->query($sql,$params);
            }elseif($key_type==1){//按姓名查询
                //教师
                if ($login_type==2){
                    $sql = "select * from tb_student where grade_id=? and name like ? ";
                    $params = array($login_user['grade_id'],'%'.$key.'%');
                }
                //管理员
                else{
                    $sql = "select * from tb_student where name like ? ";
                    $params = array('%'.$key.'%');
                }

                $result = $db->query($sql,$params);
            }
        }elseif ($_GET['action']=='select'){//显示班级学生
            $gradeid = $_GET['gradeid'];
            $sql = "select * from tb_student where grade_id=?";
            $params=array($gradeid);
            $result = $db->query($sql,$params);
        } else{  //显示所有
            $sql = "select * from tb_student";
            $result = $db->query($sql);
            // 显示教师本班级学生
            if ($login_type==2){
                $sql= "select * from tb_student where grade_id=?";
                $params = array($login_user['grade_id']);
                $result = $db->query($sql,$params);
            }elseif ($login_type==0){//学生登录只显示学生自己的信息
                $sql= "select * from tb_student where id=?";
                $params = array($login_user['id']);
                $result = $db->query($sql,$params);
            }
        }
        foreach ($result as $info){
            ?>
            <tr>
                <td><?php echo $info['id']?></td>
                <td><?php echo $info['student_no']?></td>
                <td><?php echo $info['name']?></td>
                <td><?php echo $info['gender']?></td>
                <td class="<?php echo $login_type!=1? 'hidden':''?>">
                    <a href="../view/edit_student.php?id=<?php echo $info['id']?>&flag=0" target="info">
                        <button class="btn btn-primary">修改</button>
                    </a>
                    <a href="../view/edit_student.php?id=<?php echo $info['id']?>&flag=1" target="info">
                        <button onclick="alert('是否删除？')" class="btn btn-danger">删除</button>
                    </a>
                </td>
            </tr>
            <?
        }
        ?>

    </table>